﻿using System.Data;
using System.Data.SqlClient;
using TeachingManageSystem.DAL;
using TeachingManageSystem.Entity;

namespace TeachingManageSystem.BLL
{
   public  class BookBll:DBHelper
   {
       #region  教材信息的录入
       /// <summary>
        /// 录入书本信息
        /// </summary>
        /// <param name="book"></param>
        /// <returns></returns>
       public static int InsertBook(Book book)
       {
            var sqlParameters = new SqlParameter[9];
            sqlParameters[0] = MakeInParam("@booknum", SqlDbType.NVarChar, 50, book.Number);
            sqlParameters[1] = MakeInParam("@namebook", SqlDbType.NVarChar, 50, book.Name);
            sqlParameters[2] = MakeInParam("@publishedbook", SqlDbType.NVarChar, 50, book.Publisher);
            sqlParameters[3] = MakeInParam("@publishedtimebook", SqlDbType.DateTime, 99999999, book.PublistedTime);
            sqlParameters[4] = MakeInParam("@autherbook", SqlDbType.NVarChar, 50, book.Author);
            sqlParameters[5] = MakeInParam("@pricebook", SqlDbType.Money, 50000, book.Price);
            sqlParameters[6] = MakeInParam("@booktypeid", SqlDbType.Int, 50, book.BookTypeID);
            sqlParameters[7] = MakeInParam("@picturebookid", SqlDbType.Int, 50, book.PictureID=GetLastPictureID());
            sqlParameters[8] = MakeInParam("@remarkbookn", SqlDbType.NVarChar, 500, book.Remark);
            return ProManageData("InsertBook", sqlParameters);
       }

       #region 获取在录入教材信息时上传图片的ID
       /// <summary>
       /// 
       /// </summary>
       /// <returns></returns>
       public  static int  GetLastPictureID()
       {
           var ds = ProGetData("SelectPictureOrderByRecentYime", null);
        return (int) ds.Tables[0].Rows[0][0];
       }

       #endregion

      
       /// <summary>
       /// 根据教材累呗名获取类别的ID
       /// </summary>
       /// <param name="booktypename"></param>
       /// <returns></returns>
       public static int GetBookTypeIdByName(string booktypename)
       {
           var sqlParameters = new SqlParameter[1];
           sqlParameters[0] = MakeInParam("@name", SqlDbType.NVarChar, 50, booktypename);
           return (int)ProGetData("SelectBookTyypeIDByName", sqlParameters).Tables[0].Rows[0][0];
       }

       #endregion

       #region 获取教材的所有信息
       /// <summary>
       /// 获取所有教材信息
       /// </summary>
       /// <returns></returns>
       public static DataSet GetBook()
       {
           return ProGetData("SelectAllBook", null);
       }
       #endregion

       #region 获取教材类别名
       /// <summary>
       /// 
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public static DataSet GetBookType(int id )
       {
            var sqlParamters = new SqlParameter[1];
            sqlParamters[0] = MakeInParam("@id", SqlDbType.Int, 5000,id);
            return  ProGetData("SelectBookTypeByID", sqlParamters);

       }
       #endregion

       #region 根据教材编号获取教材信息
       /// <summary>
       /// 
       /// </summary>
       /// <param name="book"></param>
       /// <returns></returns>
       public static DataSet GetBookByBookNym(Book book)
       {
           var sqlParameters = new SqlParameter[1];
           sqlParameters[0] = MakeInParam("@bookNum", SqlDbType.NVarChar, 50, book.Number);
           return ProGetData("SelectBookByBooknum", sqlParameters);
       }
       #endregion

       #region 根据教材编号更新教材信息
       /// <summary>
       /// 根据前端提供的教材编号对教材信息进行修改
       /// </summary>
       /// <param name="book"></param>
       /// <returns></returns>
       public static int UpdateBookByBooknumber(Book book)
       {
           var sqlParameters = new SqlParameter[9];
           sqlParameters[0] = MakeInParam("@booknum", SqlDbType.NVarChar, 50, book.Number);
           sqlParameters[1] = MakeInParam("@namebook", SqlDbType.NVarChar, 50, book.Name);
           sqlParameters[2] = MakeInParam("@publishedbook", SqlDbType.NVarChar, 50, book.Publisher);
           sqlParameters[3] = MakeInParam("@publishedtimebook", SqlDbType.DateTime, 99999999, book.PublistedTime);
           sqlParameters[4] = MakeInParam("@autherbook", SqlDbType.NVarChar, 50, book.Author);
           sqlParameters[5] = MakeInParam("@pricebook", SqlDbType.Money, 50000, book.Price);
           sqlParameters[6] = MakeInParam("@booktypeid", SqlDbType.Int, 50, book.BookTypeID);
           sqlParameters[7] = MakeInParam("@picturebookid", SqlDbType.Int, 50, book.PictureID = GetLastPictureID());
           sqlParameters[8] = MakeInParam("@remarkbookn", SqlDbType.NVarChar, 500, book.Remark);
           return ProManageData("UpdateBookByBookNumber", sqlParameters);
       }
       #endregion

       #region 根据教材编号删除教材信息
       /// <summary>
       /// 删除教材信息
       /// </summary>
       /// <param name="book"></param>
       /// <returns></returns>
       public static int DeleteBookBybooknym(string  booknum)
       {
           var sqlParamters = new SqlParameter[1];
           sqlParamters[0] = MakeInParam("@booknum", SqlDbType.Int, 20, booknum);
           return ProManageData("DeleteBookByID", sqlParamters);
       }
       #endregion
   }
}
